async function getList() {
  const { data: { data } } = await axios.get('/my/article/cates')
  // console.log(data);
  $('tbody').html(data.map(({ Id, name, alias }) => `
   <tr>
   <td>${name}</td>
   <td>${alias}</td>
   <td>
     <button type="button" class="layui-btn layui-btn-xs btn_edit" data-id="${Id}">编辑</button>
     <button type="button" class="layui-btn layui-btn-xs layui-btn-danger btn_delete" data-id="${Id}">删除</button>
   </td>
 </tr>
   `).join(''))

}
getList()

// 增加
let indexAddForm
$('#btnAddCate').on('click', function () {
  indexAddForm = layer.open({
    content: ` <form id="addForm" class="layui-form">
        <!-- 第一行 分类名称 -->
        <div class="layui-form-item">
          <label class="layui-form-label">分类名称</label>
          <div class="layui-input-block">
            <input type="text" name="name" required lay-verify="required" placeholder="请输入标题" autocomplete="off"
              class="layui-input">
          </div>
        </div>
        <!-- 第二行 分类别名  -->
        <div class="layui-form-item">
          <label class="layui-form-label">分类别名</label>
          <div class="layui-input-block">
            <input type="text" name="alias" required lay-verify="required" placeholder="请输入标题" autocomplete="off"
              class="layui-input">
          </div>
        </div>
        <!-- 第三行 按钮 -->
        <div class="layui-form-item">
          <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">确认添加</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
          </div>
        </div>
      </form>`,
    type: 1,
    title: '添加文章类别',
    area: ['500px', '260px']
  });

})

$('body').on('submit', '#addForm', async function (e) {
  e.preventDefault();
  const data = $(this).serialize()
  const res = await axios.post('/my/article/addcates', data)

  if (res.data.status !== 0) return layer.msg(res.data.message)

  layer.close(indexAddForm)

  getList()
  layer.msg('新增文章类别成功')

})


// 编辑
let indexEditForm
const form = layui.form
$('tbody').on('click', '.btn_edit', async function () {
  indexAddForm = layer.open({
    content: `  <form  id="editForm" class="layui-form" lay-filter="editForm">
        <!-- 第0行 存Id -->
        <div class="layui-form-item"  style="display: none;">
          <label class="layui-form-label">id</label>
          <div class="layui-input-block">
            <input type="text" name="Id" required lay-verify="required" placeholder="请输入标题" autocomplete="off"
              class="layui-input">
          </div>
        </div>
        <!-- 第一行 分类名称 -->
        <div class="layui-form-item">
          <label class="layui-form-label">分类名称</label>
          <div class="layui-input-block">
            <input type="text" name="name" required lay-verify="required" placeholder="请输入标题" autocomplete="off"
              class="layui-input">
          </div>
        </div>
        <!-- 第二行 分类别名  -->
        <div class="layui-form-item">
          <label class="layui-form-label">分类别名</label>
          <div class="layui-input-block">
            <input type="text" name="alias" required lay-verify="required" placeholder="请输入标题" autocomplete="off"
              class="layui-input">
          </div>
        </div>
        <!-- 第三行 按钮 -->
        <div class="layui-form-item">
          <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">确认修改</button>
          </div>
        </div>
      </form>
        `,
    type: 1,
    title: '修改文章类别',
    area: ['500px', '260px']
  });

  const Id = $(this).data('id')
  const { data: { data } } = await axios.get(`/my/article/cates/${Id}`)
  form.val('editForm', data)


})


$('body').on('submit', '#editForm', async function (e) {
  e.preventDefault();
  const data = $(this).serialize()
  console.log(data)
  const res = await axios.post('/my/article/updatecate', data)

  if (res.data.status !== 0) return layer.msg(res.data.message)

  layer.close(indexEditForm)

  getList()
  layer.msg('修改文章类别成功')

})


// 删除
$('tbody').on('click', '.btn_delete', async function () {
  const Id = $(this).data('id')
  console.log(Id)
  const res = await axios.get(`/my/article/delete/${Id}`)
  console.log(res)
  if (res.data.status !== 0) return layer.msg(res.data.message)
  layer.msg(res.data.message)
  getList()
})

